// EndianTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

typedef union {
	char b0, b1, b2, b3;
	int wd;
} Wb;

enum Endianness
{
    ENDIAN_BIG,
    ENDIAN_LITTLE
};


// Returns endianness of current platform
Endianness getEndianness() {
	int a = 1;
	char* bytes = (char*)&a;
	return bytes[0] == 1 ? ENDIAN_LITTLE : ENDIAN_BIG;
}

// Returns byte swapped value
int swap(int in) {
	int b = 0;

	b = (in & 0xff000000) >> 24;
	b = b | ( (in & 0x00ff0000) >> 8);
	b = b | ( (in & 0x0000ff00) << 8);
	b = b | ( (in & 0x000000ff) << 24);

	return b;
}

// Puts byte swapped <in> into <out>
void swap(int in, int* out) {
	*out = swap(in);
}

// Returns byte swapped value
float swap(float in) {
	float b = 0.0f;

	//b = (in & 0xff000000) >> 24;
	//b = b | ( (in & 0x00ff0000) >> 8);
	//b = b | ( (in & 0x0000ff00) << 8);
	//b = b | ( (in & 0x000000ff) << 24);

	return b;
}

// Puts byte swapped <in> into <out>
void swap(float in, float* out) {
	*out = swap(in);
}


void test(int in) {
	int x_int = in;
	int y_int = swap(x_int);
	int z_int = 0;
	swap(x_int, &z_int);
/*
	std::cout << x_int << " " << (unsigned int) z_int << std::endl;*/

	float x_float = *((float*)&x_int);
	float y_float = swap(x_float);
	float z_float = 0;
	swap(x_float, &z_float);
}


int main()
{	
	Endianness e = getEndianness();
	
	test(123);

	test(1);

	return 0;
}
